package cn.edu.sjtu.se.dslab.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.sjtu.se.dslab.component.design.DesignPattern;
import cn.edu.sjtu.se.dslab.component.design.DesignPatternProperty;
import cn.edu.sjtu.se.dslab.dao.IDesignPatternDao;
import cn.edu.sjtu.se.dslab.dao.IDesignPatternPropertyDao;
import cn.edu.sjtu.se.dslab.service.IDesignPatternPropertyService;

/**
 * 
 * @author longshaohang
 * 
 */
@Service("designPatternPropertyService")
@Transactional
public class DesignPatternPropertyServiceImpl implements
		IDesignPatternPropertyService {

	@Resource(name = "designPatternPropertyDao")
	private IDesignPatternPropertyDao designPatternPropertyDao;
	
	@Resource(name="designPatternDao")
	private IDesignPatternDao designPatternDao;

	@Override
	public boolean addDesignPatternProperty(
			DesignPatternProperty designPatternProperty,
			long designPatternId) {
		DesignPattern dp = designPatternDao.queryById(designPatternId);
		if (null == dp) return false;
		designPatternProperty.setCreateTime(new Date());
		designPatternProperty.setDesignPattern(dp);
		designPatternPropertyDao.save(designPatternProperty);
		return true;
	}

	@Override
	public DesignPatternProperty updateDesignPatternProperty(
			DesignPatternProperty designPatternProperty) {
		return designPatternPropertyDao.update(designPatternProperty);
	}

	@Override
	public DesignPatternProperty getDesignPatternPropertyById(long id) {
		return designPatternPropertyDao.queryById(id);
	}

	@Override
	public boolean addChildDesignPatternProperties(
			List<DesignPatternProperty> childProperties, long designPatternId) {
		DesignPattern dp = designPatternDao.queryById(designPatternId);
		if (null == dp) return false;
		for(DesignPatternProperty property : childProperties){ 
			property.setCreateTime(new Date());
			property.setDesignPattern(dp);
			designPatternPropertyDao.save(property);
		}
		return true;
	}

}
